package com.example.dirviesystem.coach.controller;

import com.example.dirviesystem.coach.dto.DrivingProgressDTO;
import com.example.dirviesystem.coach.service.DrivingProgressService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/progress")
@RequiredArgsConstructor
@CrossOrigin(origins = "http://localhost:5173") // 允许前端跨域访问
public class DrivingProgressController {

    private final DrivingProgressService progressService;

    @GetMapping
    public ResponseEntity<List<DrivingProgressDTO>> getAllProgress(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String name) {
        Pageable pageable = PageRequest.of(page, size);
        return ResponseEntity.ok(progressService.getAllProgress(name, pageable));
    }

    @PutMapping
    public ResponseEntity<DrivingProgressDTO> updateProgress(@RequestBody DrivingProgressDTO dto) {
        return ResponseEntity.ok(progressService.updateProgress(dto));
    }
}